const unifiedRes = {
    code: 1,
    data: null,
    msg: "请求失败",
};
const unifiedHeader = {
    contentType: "json",
    dataType: "json",
};
module.exports = {
    /**
     * @description http get 请求
     * @method GET
     * @param {String} url 请求地址
     * @param {Object} params 参数
     */
    async get(url, params) {
        console.log(params)
        const result = await this.ctx.curl(url, {
            data: params,
            method: "GET",
            ...unifiedHeader,
        });
        return this.handleData(result);
    },

    /**
     * @description http post 请求
     * @method POST
     * @param {String} url 请求地址
     * @param {Object} params 参数
     */
    async post(url, params) {
        const result = await this.ctx.curl(url, {
            method: "POST",
            data: params,
            ...unifiedHeader,
        });
        return this.handleData(result);
    },

    /**
     * @description http post 请求
     * @method PUT
     * @param {String} url 请求地址
     * @param {Object} params 参数
     */
    async put(url, params) {
        const result = await this.ctx.curl(url, {
            method: "PUT",
            data: params,
            ...unifiedHeader,
        });
        return this.handleData(result);
    },

    handleData(response) {
        console.log(response)
        if (!response) return unifiedRes;
        if (response.status == 200) {
            return response.data;
        } else {
            return unifiedRes;
        }
    },
    // 业务成功统一返回结构
    ormSuccess(data = null) {
        return {
            data,
            success: true,
        };
    },
    // 业务失败统一返回结构
    ormError(msg = "系统错误", data = null) {
        console.log('服务逻辑处理失败',msg)
        return {
            msg: msg,
            success: false,
            data,
        };
    },
}